﻿using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace DAL
{
    public class NhanVienDAL
    {
         private SqlConnection cn = null;
         public NhanVienDAL()
        {
            string stringConnection = ConfigurationSettings.AppSettings["connectionstring"].ToString();
            cn = new SqlConnection(stringConnection);
            cn.Open();
        }

        public void INSERT(NhanVien D)
        {
            SqlCommand cm = new SqlCommand();
            cm.CommandText = "sp_NHANVIEN_Insert";
            cm.Connection = this.cn;
            cm.CommandType = CommandType.StoredProcedure;

            cm.Parameters.AddWithValue("@TenDangNhap_NhanVien", D.TenDangNhap_NhanVien);
            cm.Parameters.AddWithValue("@MatKhau_NhanVien", D.MatKhau_NhanVien);
            cm.Parameters.AddWithValue("@TenNhanVien", D.TenNhanVien);
            cm.Parameters.AddWithValue("@DiaChi", D.DiaChi);
            cm.Parameters.AddWithValue("@DienThoai", D.DienThoai);
            cm.Parameters.AddWithValue("@Email", D.Email);
            cm.Parameters.AddWithValue("@MaPhanQuyen", D.MaPhanQuyen);
            cm.Parameters.AddWithValue("@TrangThai", D.TrangThai);

            cm.ExecuteNonQuery();
        }

        public void UPDATE(NhanVien D)
        {
            SqlCommand cm = new SqlCommand();
            cm.CommandText = "sp_NHANVIEN_Update";
            cm.Connection = this.cn;
            cm.CommandType = CommandType.StoredProcedure;

            cm.Parameters.AddWithValue("@TenDangNhap_NhanVien", D.TenDangNhap_NhanVien);
            cm.Parameters.AddWithValue("@MatKhau_NhanVien", D.MatKhau_NhanVien);
            cm.Parameters.AddWithValue("@TenNhanVien", D.TenNhanVien);
            cm.Parameters.AddWithValue("@DiaChi", D.DiaChi);
            cm.Parameters.AddWithValue("@DienThoai", D.DienThoai);
            cm.Parameters.AddWithValue("@Email", D.Email);
            cm.Parameters.AddWithValue("@MaPhanQuyen", D.MaPhanQuyen);
            cm.Parameters.AddWithValue("@TrangThai", D.TrangThai);

            cm.ExecuteNonQuery();
        }

        public void UpdateTrangThai(string TenDangNhap_NhanVien, int TrangThai)
        {
            SqlCommand command = new SqlCommand("sp_NhanVien_UpdateTrangThai", cn);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.AddWithValue("@TenDangNhap_NhanVien", TenDangNhap_NhanVien);
            command.Parameters.AddWithValue("@TrangThai", TrangThai);
            command.ExecuteNonQuery();

        }

        public void DELETE(string iD)
        {
            SqlCommand cm = new SqlCommand();
            cm.CommandText = "sp_NHANVIEN_Delete";
            cm.Connection = this.cn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenDangNhap_NhanVien", iD);
            cm.ExecuteNonQuery();
        }

        public NhanVienCollection GET(string Where, int PageIndex, int PageSize, string OrderBy, string OrderDirection, out int TotalRecords)
        {
            SqlCommand cm = new SqlCommand();
            cm.CommandText = "sp_NHANVIEN_Get";
            cm.Connection = this.cn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", PageIndex);
            cm.Parameters.AddWithValue("@PageSize", PageSize);
            cm.Parameters.AddWithValue("@OrderBy", OrderBy);
            cm.Parameters.AddWithValue("@OrderDirection", OrderDirection);
            SqlParameter param = new SqlParameter("@TotalRecords", DbType.Int32);
            param.Direction = ParameterDirection.Output;
            cm.Parameters.Add(param);
            SqlDataAdapter da = new SqlDataAdapter(cm);
            DataSet ds = new DataSet();
            da.Fill(ds);
            NhanVienCollection NhanVienCollection = new NhanVienCollection();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                NhanVien D = new NhanVien();
                D.TenDangNhap_NhanVien = ds.Tables[0].Rows[i]["TenDangNhap_NhanVien"].ToString();
                D.MatKhau_NhanVien = ds.Tables[0].Rows[i]["MatKhau_NhanVien"].ToString();
                D.TenNhanVien = ds.Tables[0].Rows[i]["TenNhanVien"].ToString();
                D.DiaChi = ds.Tables[0].Rows[i]["DiaChi"].ToString();
                D.DienThoai = ds.Tables[0].Rows[i]["DienThoai"].ToString();
                D.Email = ds.Tables[0].Rows[i]["Email"].ToString();
                D.MaPhanQuyen = int.Parse(ds.Tables[0].Rows[i]["MaPhanQuyen"].ToString());
                D.TrangThai = int.Parse(ds.Tables[0].Rows[i]["TrangThai"].ToString());

                NhanVienCollection.Add(D);
            }
            TotalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return NhanVienCollection;
        }
    }
}
